var createCache=[];
var vtag=function(){
    if(typeof arguments[0]=="object" && typeof arguments[0].view=="function"){
        if(typeof arguments[0].oninit=="function"){
            arguments[0].oninit();
        }
        if(typeof arguments[0].oncreate=="function"){
            createCache.push(arguments[0].oncreate);
        }
        var cnode=arguments[0].view();
        if(typeof arguments[1]=="object") cnode.attrs=arguments[1];
        return cnode;
        
    }
    if(arguments.length==2){
        return {tag:arguments[0],attrs:{},children:arguments[1]}
    }

    return {tag:arguments[0],attrs:arguments[1],children:arguments[2]}
}

var render=function(dom,vnode){
    //开始绘制
    dom.innerHTML="";
    renderHandler(dom,vnode);
    for(var key in createCache){
        createCache[key]();
    }
    createCache=[];
}
var renderHandler=function(dom,vnode){
    if(Array.isArray(vnode)){
        for(var i=0;i<vnode.length;i++){
            //递归
            renderHandler(dom,vnode[i]);
        }
        return dom;
    }
    var truthTag=document.createElement(vnode.tag);
    for(var key in vnode.attrs){
        if(key=="onclick" || key=="oninput" || key=="onchange" || key=="onmouseover" || key=="onmouseout"){
            truthTag.addEventListener(key.replace("on",""),vnode.attrs[key]);
            continue;
        }
        truthTag.setAttribute(key,vnode.attrs[key]);
    }
    if(typeof vnode.children=="string"){
        truthTag.innerText=vnode.children;
        dom.appendChild(truthTag);
        return dom;
    }
    else if(typeof vnode.children=="object"){
        dom.appendChild(truthTag);
        //递归
        renderHandler(truthTag,vnode.children);
        return dom
    }
}

m=function(){
    return vtag.apply(this,arguments);
}
m.render=render;

if (typeof module !== "undefined")
    module["exports"] = m
else 
    window.m = m